package com.rt.easyjava.common.base;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.*;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by wxy on 22/01/09.
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = false)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BaseV3Bean<T extends Model<?>> extends Model<T> {
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    private Long creatorId;
    private String creator;
    private Date createTime;

    private Long updatorId;
    private String updator;
    private Date updateTime;

    private Long companyId;
    private Integer deleteFlag;
    @Version
    private Integer version;

    // 重写这个方法，return当前类的主键
    @Override
    protected Serializable pkVal() {
        return id;
    }

    // set deleteflag default 0, 查询参数时，默认只查有效数据
    public void setDeleteflagDefaultZero(){
        if(this.deleteFlag==null){
            this.setDeleteFlag(0);
        }
    }

    // 数据访问边界 0：全部数据， 10：机构内部数据
    public void rebuildDataLimitQueryBean(UserDetailsDto userDetailsDto){
        if (userDetailsDto==null) return;
        int dataLimitType = userDetailsDto.getDataLimitType();
        if (dataLimitType >= 10) {
            this.setCompanyId(Long.parseLong(userDetailsDto.getCompanyId()));
        }
        if (dataLimitType >= 20) {
            this.setCreatorId(Long.parseLong(userDetailsDto.getUserId()));
        }
    }


    public void setCreatorUpdatorInfo(String userDetailsJson) {
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "");
        this.setCreatorId(Long.parseLong(userDetailsDto.getUserId()));
        this.setCreator(userDetailsDto.getUserName());
        this.setUpdatorId(Long.parseLong(userDetailsDto.getUserId()));
        this.setUpdator(userDetailsDto.getUserName());
        this.setCompanyId(Long.parseLong(userDetailsDto.getCompanyId()));
        this.setCreateTime(null);
        this.setUpdateTime(null);
    }
    public void setUpdatorInfo(String userDetailsJson) {
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "");
        this.setUpdatorId(Long.parseLong(userDetailsDto.getUserId()));
        this.setUpdator(userDetailsDto.getUserName());
        this.setCreatorId(null);
        this.setCreator(null);
        this.setCompanyId(null);
        this.setCreateTime(null);
        this.setUpdateTime(null);
    }
}
